/*
 * Copyright (c) 2022-2022 All rights Reserved.
 */

package com.driving.core.controller;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.driving.core.base.BaseController;
import com.driving.core.entity.Video;
import com.driving.core.entity.VideoCategory;
import com.driving.core.exception.DrivingException;
import com.driving.core.service.IVideoService;
import com.driving.core.util.Constant;
import com.driving.core.util.JsonWebToken;
import com.driving.core.util.MessageType.ExceptionType;
import com.driving.core.util.Response;

import lombok.extern.slf4j.Slf4j;

/**
 * @author SAGITTAR
 * @since 2017
 */
@Slf4j
@RestController
@RequestMapping(value = "/video")
public class VideoController extends BaseController<IVideoService, Video> {

    @GetMapping(value = "/name/{name}")
    public Response<Video> getVideoByName(@PathVariable String name) {
        Video video = baseService.selectByName(name);
        if (Objects.isNull(video)) {
            throw new DrivingException(configReader.getExceptionMessage(ExceptionType.VIDEO_NOT_FOUND, name));
        }
        log.info("查询名称为{}，视频路径为{}", name, video.getFile());
        return Response.success(video);
    }

    @GetMapping(value = "/id/{id}")
    public Response<Video> getVideoById(@PathVariable Integer id) {
        Video video = baseService.getById(id);
        if (Objects.isNull(video)) {
            throw new DrivingException(configReader.getExceptionMessage(ExceptionType.VIDEO_NOT_FOUND, id));
        }
        log.info("查询名称为{}，视频路径为{}", video.getName(), video.getFile());
        return Response.success(video);
    }

    /**
     * 获取分类列表
     *
     * @param subjectId 科目几
     */
    @GetMapping(value = "/category/list/{subjectId}")
    public Response<List<VideoCategory>> getCategoryList(@PathVariable int subjectId) {
        if (subjectId <= 0) {
            return Response.failure(configReader.getExceptionMessage(ExceptionType.INCORRECT_PARAMETERS));
        }
        return Response.success(baseService.selectListBySubjectId(subjectId));
    }

    /**
     * 获取该科目的视频列表
     *
     * @param subjectId subjectId
     * @return 获取该科目的视频列表
     */
    @GetMapping(value = "/list-by-subject/{subjectId}")
    public Response<List<Map<String, String>>> getVideoListBySubjectId(@PathVariable int subjectId) {
        return Response.success(baseService.selectListBySubjectIdOrCategoryId(1, subjectId, false));
    }

    /**
     * 获取该分类下的视频列表
     *
     * @param request request
     * @param categoryId categoryId
     * @return 获取该分类下的视频列表
     */
    @GetMapping(value = "/list-by-category/{categoryId}")
    public Response<List<Map<String, String>>> getVideoListByCategoryId(HttpServletRequest request,
            @PathVariable int categoryId) {
        Map<String, String> token = JsonWebToken.parseStudent(request.getHeader(Constant.TOKEN));
        return Response.success(baseService.selectListBySubjectIdOrCategoryId(2, categoryId, Objects.nonNull(token)));
    }
}
